﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Resources;
using System.Collections;
using System.Diagnostics;

namespace ResourceTranslator
{
    public static class ResourcesUtil
    {
        public static ResourceData[] ReadTextResources(string txtFile)
        {
            List<ResourceData> resources = new List<ResourceData>();
            using (StreamReader reader = File.OpenText(txtFile))
            {
                while (true)
                {
                    string line = reader.ReadLine();
                    if (line == null)
                        break;
                    int indexOfSplitter = line.IndexOf('=');
                    string key = line.Substring(0, indexOfSplitter);
                    string value = line.Substring(indexOfSplitter + 1, line.Length - indexOfSplitter - 1);
                    resources.Add(new ResourceData { Key = key, Value = value });
                }
            }
            return resources.ToArray();
        }
        public static void WriteTextResources(ResourceData[] resources, string resxFile)
        {
            using (StreamWriter reader = new StreamWriter(resxFile))
            {
                foreach (ResourceData resource in resources)
                    reader.WriteLine(resource.Key + "=" + resource.Value);
            }
        }
        public static void WriteResxResources(ResourceData[] resources, string resxFile)
        {
            using (ResXResourceWriter reader = new ResXResourceWriter(resxFile))
            {
                foreach (ResourceData resource in resources)
                {
                    ResXDataNode dataNode = new ResXDataNode(resource.Key, resource.Value) { Comment = resource.Comments };

                    reader.AddResource(dataNode);
                }
            }
        }

        public static ResourceData[] ReadResxResources(string resxFile)
        {
            List<ResourceData> resources = new List<ResourceData>();
            using (ResXResourceReader reader = new ResXResourceReader(resxFile))
            {
                reader.UseResXDataNodes = true;
                IDictionaryEnumerator enumerator = reader.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string key = enumerator.Key as string;
                    ResXDataNode dataNode = (ResXDataNode)enumerator.Value;
                    bool validData;
                    string value = TryGetString(dataNode, out validData);
                    if (key == null || !validData)
                    {
                        Trace.WriteLine(String.Format("Key: {0}, Value: {1}", key, value));
                        continue;
                    }
                    resources.Add(new ResourceData { Key = key, Value = value, Comments = dataNode.Comment });
                }
            }
            return resources.ToArray();
        }
        public static ResourceData[] ReadBinaryResources(string resourceFile)
        {
            List<ResourceData> resources = new List<ResourceData>();
            using (ResourceReader reader = new ResourceReader(resourceFile))
            {
                IDictionaryEnumerator enumerator = reader.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string key = enumerator.Key as string;
                    string value = enumerator.Value as string;
                    if (key == null || value == null)
                    {
                        Trace.WriteLine(String.Format("Key: {0}, Value: {1}", key, value));
                        continue;
                    }
                    resources.Add(new ResourceData { Key = key, Value = value });
                }
            }
            return resources.ToArray();
        }
        public static void WriteBinaryResources(ResourceData[] resources, string resourceFile)
        {
            using (ResourceWriter reader = new ResourceWriter(resourceFile))
            {
                foreach (ResourceData resource in resources)
                {
                    reader.AddResource(resource.Key, resource.Value);
                }
            }
        }

        static string TryGetString(ResXDataNode dataNode, out bool success)
        {
            try
            {
                object data = dataNode.GetValue(new System.Reflection.AssemblyName[] { new System.Reflection.AssemblyName("mscorlib") });
                string str = Convert.ToString(data);
                success = true;
                return str;
            }
            catch
            {
                success = false;
            }
            return null;
        }
    }
}
